﻿using CommonCs;
using LogicServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;

namespace BugSysSolution.RequestAjaxPage
{
    public partial class BugTypeAjaxPage : System.Web.UI.Page
    {
        

        protected void Page_Load(object sender, EventArgs e)
        {
            String tag = Request["tag"] ?? String.Empty;
            if (tag == "add")
            {
                AddType();
            }
            if (tag == "edit")
            {
                EditType();
            }
            if (tag == "del")
            {
                DelType();
            }
            if (tag == "getalltype")
            {
                GetAllTypes();
            }
        }

        private void AddType()
        {
            Users user = Session["user"] as Users;
            String val = Request.Form["val"] ?? String.Empty;
            BugTypesLogic bl = new BugTypesLogic();
            bool result = bl.AddBugType(val);
            if (result)
            {
                Response.Write("Success");
                Common.WriteFile(CommonCs.Exceptions.Operational, user.Uid, "添加bug_type[" + val + "]成功!");
            }
            else
            {
                Response.Write("Error");
                Common.WriteFile(CommonCs.Exceptions.Operational, user.Uid, "添加bug_type[" + val + "]失败!");
            }
        }

        private void EditType()
        {
            Users user = Session["user"] as Users;
            String id = Request.Form["id"] ?? String.Empty;
            String val = Request.Form["val"] ?? String.Empty;
            BugTypesLogic bl = new BugTypesLogic();
            bool result = bl.EditBugType(id, val);
            if (result)
            {
                Response.Write("Success");
                Common.WriteFile(CommonCs.Exceptions.Operational, user.Uid, "修改bug_type[" + val + "]成功!");
            }
            else
            {
                Response.Write("Error");
                Common.WriteFile(CommonCs.Exceptions.Operational, user.Uid, "修改bug_type[" + val + "]失败!");
            }
        }

        private void DelType()
        {
            Users user = Session["user"] as Users;
            String id = Request.Form["id"] ?? String.Empty;
            String val = Request.Form["val"] ?? String.Empty;
            BugTypesLogic bl = new BugTypesLogic();
            bool result = bl.DelBugType(id);
            if (result)
            {
                Response.Write("Success");
                Common.WriteFile(CommonCs.Exceptions.Operational, user.Uid, "删除bug_type[" + val + "]成功!");
            }
            else
            {
                Response.Write("Error");
                Common.WriteFile(CommonCs.Exceptions.Operational, user.Uid, "删除bug_type[" + val + "]失败!");
            }
        }

        public void GetAllTypes()
        {
            BugTypesLogic bl = new BugTypesLogic();
            string json = string.Empty;

            json = Common.DataTableToJson(Common.ListToDataTable<Model.BugTypes>(bl.GetAllType()));

            Response.Write(json);
        }
    }
}